﻿/// Write a program that reads a text file containing a square matrix of numbers and finds in the matrix an area of size 2 x 2
/// with a maximal sum of its elements. The first line in the input file contains the size of matrix N.
/// Each of the next N lines contain N numbers separated by space. The output should be a single number in a separate text file. Example:
// 4
// 2 3 3 4
// 0 2 3 4			
// 3 7 1 2
// 4 3 3 2
///
// => 17

using System;
using System.IO;


namespace c_sh_II_lek_6_files
{
    class zad5
    {
        static void Main()
        {
            string input = @"..\..\input.txt";
            string line;
            int n;
            int[,] arr;

            using (StreamReader sr = new StreamReader(input)) 
            {
                line = sr.ReadLine();
                n = int.Parse(line);
                arr = new int[n,n];

                for (int i = 0; i < n; i++)
                {
                    string[] strNum =sr.ReadLine().Split();

                    for (int j = 0; j < strNum.Length; j++)
                    {
                        arr[i,j] = int.Parse(strNum[j]);
                    }
                }

            }

            Console.WriteLine(GetMaxTwoByTwo(arr));
        }

        static int GetMaxTwoByTwo(int[,] matrix)
        {
            int maxSum = int.MinValue;

            for (int i = 0; i < matrix.GetLength(0) - 1; i++)
            {
                for (int j = 0; j < matrix.GetLength(1) - 1; j++)
                {
                    maxSum = Math.Max(maxSum, matrix[i, j] + matrix[i + 1, j] + matrix[i, j + 1] + matrix[i + 1, j + 1]);
                }
            }

            return maxSum;
        }
    }
}
